package com.hxit.huaxiamelongarden.controller;

import com.hxit.huaxiamelongarden.domin.dto.LoginDto;
import com.hxit.huaxiamelongarden.domin.po.User;
import com.hxit.huaxiamelongarden.service.UserService;
import com.hxit.huaxiamelongarden.utils.JwtUtil;
import com.hxit.huaxiamelongarden.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName Login
 * @Author 12694
 * @Date 2025/1/20 14:49
 * @Version 1.0
 **/
@RestController
@Slf4j
@RequestMapping("/login")
@CrossOrigin(origins = "*")  // 允许所有来源
public class LoginController {
    @Autowired
    private UserService userService;
    @PostMapping
    public Result login(@RequestBody LoginDto loginDto){
      log.info("用户登录{}",loginDto);
        User user = userService.selectByLoginDto(loginDto);
        if (user == null){
            return Result.error("用户不存在！");
        }
        if (!user.getPassword().equals(loginDto.getPassword())){
            return Result.error("密码错误！");
        }
        return Result.success(JwtUtil.generateToken(user.getUserId().toString()));
    }
}
